package com.xintoucloud.gsv.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.xintoucloud.gsv.entity.SuperVision;

public interface SuperVisionMapper {
	/**
	 * 保存一条督办信息
	 * @param superVision
	 */
	@Insert("INSERT INTO t_supervision(svTitle,svContent,startTime,confirmTime,endTime,sendLeader,replices,sid,did,state) values(#{svTitle},#{svContent},"
			+ "#{startTime},#{confirmTime},#{endTime},#{sendLeader},#{replices},#{supervisionType.sid},#{dept.did},#{superVisionState.stid})")
	void save(SuperVision superVision);
	/**
	 * 查询所有的督办信息
	 * @return
	 */
	@Select("select * from t_supervision")
	@Results({
		@Result(property="supervisionType",column="sid",one=@One(select="com.xintoucloud.gsv.mapper.SupervisionTypeMapper.getSupervisionType")),
		@Result(property="dept",column="did",one=@One(select="com.xintoucloud.gsv.mapper.DeptMapper.getDept")),
		@Result(property="superVisionState",column="state",one=@One(select="com.xintoucloud.gsv.mapper.SuperVisionStateMapper.getSuperVisionState"))
	})
	List<SuperVision> list();
	/**
	 * 分页查询
	 * @return
	 */
	@Select("select * from t_supervision limit #{pageIndex},#{size}")
	@Results({
		@Result(property="supervisionType",column="sid",one=@One(select="com.xintoucloud.gsv.mapper.SupervisionTypeMapper.getSupervisionType")),
		@Result(property="dept",column="did",one=@One(select="com.xintoucloud.gsv.mapper.DeptMapper.getDept")),
		@Result(property="superVisionState",column="state",one=@One(select="com.xintoucloud.gsv.mapper.SuperVisionStateMapper.getSuperVisionState"))
	})
	List<SuperVision> listPage(@Param("pageIndex")int pageIndex,@Param("size")int size);
	/**
	 * 分页查询
	 * @return
	 */
	@Select("select * from t_supervision where state="+1+" limit #{pageIndex},#{size}")
	@Results({
		@Result(property="supervisionType",column="sid",one=@One(select="com.xintoucloud.gsv.mapper.SupervisionTypeMapper.getSupervisionType")),
		@Result(property="dept",column="did",one=@One(select="com.xintoucloud.gsv.mapper.DeptMapper.getDept")),
		@Result(property="superVisionState",column="state",one=@One(select="com.xintoucloud.gsv.mapper.SuperVisionStateMapper.getSuperVisionState"))
	})
	List<SuperVision> listDraftPage(@Param("pageIndex")int pageIndex,@Param("size")int size);
	/**
	 * 分页查询
	 * @return
	 */
	@Select("select * from t_supervision where state<="+2+" limit #{pageIndex},#{size}")
	@Results({
		@Result(property="supervisionType",column="sid",one=@One(select="com.xintoucloud.gsv.mapper.SupervisionTypeMapper.getSupervisionType")),
		@Result(property="dept",column="did",one=@One(select="com.xintoucloud.gsv.mapper.DeptMapper.getDept")),
		@Result(property="superVisionState",column="state",one=@One(select="com.xintoucloud.gsv.mapper.SuperVisionStateMapper.getSuperVisionState"))
	})
	List<SuperVision> listWaitRegisterPage(@Param("pageIndex")int pageIndex,@Param("size")int size);
	/**
	 * 分页查询
	 * @return
	 */
	@Select("select * from t_supervision where state="+3+" and did=#{did} limit #{pageIndex},#{size}")
	@Results({
		@Result(property="supervisionType",column="sid",one=@One(select="com.xintoucloud.gsv.mapper.SupervisionTypeMapper.getSupervisionType")),
		@Result(property="dept",column="did",one=@One(select="com.xintoucloud.gsv.mapper.DeptMapper.getDept")),
		@Result(property="superVisionState",column="state",one=@One(select="com.xintoucloud.gsv.mapper.SuperVisionStateMapper.getSuperVisionState"))
	})
	List<SuperVision> listWaitDirectReply(@Param("pageIndex")int pageIndex,@Param("size")int size,@Param("did")int did);
	/**
	 * 分页查询
	 * @return
	 */
	@Select("select * from t_supervision s,t_dept d where d.did=s.did and d.cmleader_id=#{uid} and state="+5+" limit #{pageIndex},#{size}")
	@Results({
		@Result(property="supervisionType",column="sid",one=@One(select="com.xintoucloud.gsv.mapper.SupervisionTypeMapper.getSupervisionType")),
		@Result(property="dept",column="did",one=@One(select="com.xintoucloud.gsv.mapper.DeptMapper.getDept")),
		@Result(property="superVisionState",column="state",one=@One(select="com.xintoucloud.gsv.mapper.SuperVisionStateMapper.getSuperVisionState"))
	})
	List<SuperVision> listWaitLeaderReply(@Param("pageIndex")int pageIndex,@Param("size")int size,@Param("uid")int uid);
	/**
	 * 分页查询
	 * @return
	 */
	@Select("select * from t_supervision where state="+4+" and did=#{did} limit #{pageIndex},#{size}")
	@Results({
		@Result(property="supervisionType",column="sid",one=@One(select="com.xintoucloud.gsv.mapper.SupervisionTypeMapper.getSupervisionType")),
		@Result(property="dept",column="did",one=@One(select="com.xintoucloud.gsv.mapper.DeptMapper.getDept")),
		@Result(property="superVisionState",column="state",one=@One(select="com.xintoucloud.gsv.mapper.SuperVisionStateMapper.getSuperVisionState"))
	})
	List<SuperVision> listWaitDutyConfirm(@Param("pageIndex")int pageIndex,@Param("size")int size,@Param("did")int did);
	
	/**
	 * 分页查询
	 * @return
	 */
	@Select("select * from t_supervision where state="+6+" limit #{pageIndex},#{size}")
	@Results({
		@Result(property="supervisionType",column="sid",one=@One(select="com.xintoucloud.gsv.mapper.SupervisionTypeMapper.getSupervisionType")),
		@Result(property="dept",column="did",one=@One(select="com.xintoucloud.gsv.mapper.DeptMapper.getDept")),
		@Result(property="superVisionState",column="state",one=@One(select="com.xintoucloud.gsv.mapper.SuperVisionStateMapper.getSuperVisionState"))
	})
	List<SuperVision> listWaitVisionConfirm(@Param("pageIndex")int pageIndex,@Param("size")int size);
	
	/**
	 * 分页查询
	 * @return
	 */
	@Select("select * from t_supervision where state="+2+" and did=#{did} limit #{pageIndex},#{size}")
	@Results({
		@Result(property="supervisionType",column="sid",one=@One(select="com.xintoucloud.gsv.mapper.SupervisionTypeMapper.getSupervisionType")),
		@Result(property="dept",column="did",one=@One(select="com.xintoucloud.gsv.mapper.DeptMapper.getDept")),
		@Result(property="superVisionState",column="state",one=@One(select="com.xintoucloud.gsv.mapper.SuperVisionStateMapper.getSuperVisionState"))
	})
	List<SuperVision> listMessagePage(@Param("pageIndex")int pageIndex,@Param("size")int size,@Param("did")int did);
	/**
	 * 查询总条数
	 * @return
	 */
	@Select("select count(*) from t_supervision")
	long count();
	/**
	 * 查询某状态条数
	 * @return
	 */
	@Select("select count(state) from t_supervision where state=#{stid}")
	long countStid(int stid);
	/**
	 * 查询草稿箱总条数
	 * @return
	 */
	@Select("select count(*) from t_supervision where state="+1+"")
	long countDraft();
	/**
	 * 删除督办信息
	 */
	@Delete("delete from t_supervision where svid=#{svid}")
	void deleteSuperVision(int svid);
	/**
	 * 查询督办信息
	 * @param svid
	 * @return
	 */
	@Select("select * from t_supervision where svid=#{svid}")
	SuperVision getSuperVision(int svid);
	/**
	 * 更新督办信息
	 * @param SuperVision
	 */
	@Update("update t_supervision set svTitle=#{svTitle},svContent=#{svContent},startTime=#{startTime},confirmTime=#{confirmTime},endTime=#{endTime},sendLeader=#{sendLeader},replices=#{replices},sid=#{supervisionType.sid},did=#{dept.did},state=#{superVisionState.stid},realConfirmTime=#{realConfirmTime} where svid=#{svid}")	
	void update(SuperVision SuperVision);
	/**
	 * 修改签收时间
	 * @param svid
	 * @param endTime
	 */
	@Update("update t_supervision set confirmTime=#{param2} where svid=#{param1}")
	void updateTime(int svid,String confirmTime);
	/**
	 * 更新督办状态
	 * @param SuperVision
	 */
	@Update("update t_supervision set state=#{stid} where svid=#{svid}")	
	void updateStid(@Param("stid")int stid,@Param("svid")int svid);
	
	/**
	 * 根据部门名称查询部门详细内容
	 * @param dname
	 */
	@Select("select * from t_supervision where dname=#{dname}")
	void selectDepartment(String dname);
}
